package com.smartleanx.module.hrm.controller.app.employee;

import com.smartleanx.framework.common.pojo.CommonResult;
import com.smartleanx.framework.common.util.object.BeanUtils;
import com.smartleanx.framework.security.core.annotations.PreAuthenticated;
import com.smartleanx.module.hrm.context.EmployeeContextHolder;
import com.smartleanx.module.hrm.controller.admin.employee.work.EmployeeWorkExperienceRespVO;
import com.smartleanx.module.hrm.controller.admin.employee.work.EmployeeWorkExperienceSaveReqVO;
import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeWorkExperienceDO;
import com.smartleanx.module.hrm.service.employee.EmployeeWorkExperienceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static com.smartleanx.framework.common.pojo.CommonResult.success;

@Tag(name = "用户APP - 员工工作经历")
@RestController
@RequestMapping("/hrm/employee-work-experience")
@Validated
public class APPEmployeeWorkExperienceController {

    @Resource
    private EmployeeWorkExperienceService employeeWorkExperienceService;

    @PostMapping("/create")
    @Operation(summary = "创建员工工作经历")
    @PreAuthenticated
    public CommonResult<Long> createEmployeeWorkExperience(@Valid @RequestBody EmployeeWorkExperienceSaveReqVO createReqVO) {
        return success(employeeWorkExperienceService.createEmployeeWorkExperience(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新员工工作经历")
    @PreAuthenticated
    public CommonResult<Boolean> updateEmployeeWorkExperience(@Valid @RequestBody EmployeeWorkExperienceSaveReqVO updateReqVO) {
        employeeWorkExperienceService.updateEmployeeWorkExperience(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除员工工作经历")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthenticated
    public CommonResult<Boolean> deleteEmployeeWorkExperience(@RequestParam("id") Long id) {
        employeeWorkExperienceService.deleteEmployeeWorkExperience(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得员工工作经历")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthenticated
    public CommonResult<EmployeeWorkExperienceRespVO> getEmployeeWorkExperience(@RequestParam("id") Long id) {
        EmployeeWorkExperienceDO employeeWorkExperience = employeeWorkExperienceService.getEmployeeWorkExperience(id);
        return success(BeanUtils.toBean(employeeWorkExperience, EmployeeWorkExperienceRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "根据员工id获得工作经历列表")
    @PreAuthenticated
    public CommonResult<List<EmployeeWorkExperienceRespVO>> getEmployeeWorkExperienceList() {
        Long employeeId = EmployeeContextHolder.getEmployeeId();
        List<EmployeeWorkExperienceDO> workExperienceList = employeeWorkExperienceService.getEmployeeWorkExperienceList(employeeId);
        return success(BeanUtils.toBean(workExperienceList, EmployeeWorkExperienceRespVO.class));
    }

}
